package com.kehongborui.fliggy.interceptor;

import com.kehongborui.fliggy.enums.ModuleEnum;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-09-11
 */
public class CurrentModuleInterceptor extends HandlerInterceptorAdapter {
//    user,  // 用户管理    -> 1
//    train, // 火车票模块  -> 2
//    air,   // 机票模块    -> 3
//    trip,  // 我的旅行    -> 4
//    about, // 关于我们    -> 5
//    manage, // 管理模块（后台）    -> 6

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI(); // 获取路径

        int currentModule = 0;
        if (uri.contains(ModuleEnum.user.name())) {
            currentModule = 1;
        } else if (uri.contains(ModuleEnum.train.name())) {
            currentModule = 2;
        } else if (uri.contains(ModuleEnum.air.name())) {
            currentModule = 3;
        } else if (uri.contains(ModuleEnum.trip.name())) {
            currentModule = 4;
        } else if (uri.contains(ModuleEnum.about.name())) {
            currentModule = 5;
        } else if (uri.contains(ModuleEnum.manage.name())) {
            currentModule = 6;
        } else if (uri.contains(ModuleEnum.search.name())) {
            currentModule = 7;
        } else if (uri.equals("/")) {
            currentModule = 2;
        }
        request.getSession().setAttribute(ModuleEnum.CURRENT_MODULE.name(), currentModule);

        return true;
    }
}
